/**
 * 
 */
package com.seeyon.apps.cfca.tools;

import cfca.logback.util.LogbackInitializer;
import cfca.paperless.base.util.IPUtils;
import cfca.paperless.base.util.StringUtil;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Description CFCA专用日志组件加载工具，客户集成时需要在系统启动阶段加载。（此处仅为参考示例）
 * @Author tiansong
 * @Date 2019-8-5
 * @CodeReviewer liujiancheng
 */
public class CfcaLogbackInitializer {
    
    public static final cfca.org.slf4j.Logger logger = cfca.org.slf4j.LoggerFactory.getLogger("environmentLogger");

    /**
     * 初始化方法，使用默认设定值
     * 默认设定值1：externalConfigFilePath = [./config/logback-cfca.xml]
     * 默认设定值2：applicationName = [PaperlessClient-log-{IP}]
     * @throws Exception
     */
    public static void init() throws Exception {
        init(null, null);
    }

    /**
     * 初始化方法，优先使用客户指定参数值，指定参数为空时使用默认设定值
     * 默认设定值1：externalConfigFilePath = [./config/logback-cfca.xml]
     * 默认设定值2：applicationName = [PaperlessClient-log-{IP}]
     * @throws Exception 
     */
    public static void init(String externalConfigFilePath, String applicationName) throws Exception {
        
        try{
            // 参数配置文件的存放目录
            if (StringUtil.isEmpty(externalConfigFilePath)) {
                externalConfigFilePath = "./config/logback-cfca.xml";
            }
            System.out.println("externalConfigFilePath=" + externalConfigFilePath);
            
            File externalConfigFile = new File(externalConfigFilePath);
            if (!externalConfigFile.exists()) {
                throw new IOException("Logback External Config File Parameter does not reference a file that exists");
            }
            if (!externalConfigFile.isFile()) {
                throw new IOException("Logback External Config File Parameter exists, but does not reference a file");
            }
            if (!externalConfigFile.canRead()) {
                throw new IOException("Logback External Config File exists and is a file, but cannot be read.");
            }
            
            externalConfigFilePath = new File(externalConfigFilePath).getCanonicalPath();
            System.out.println("externalConfigFilePath.getCanonicalPath=" + externalConfigFilePath);
            
            if (StringUtil.isEmpty(applicationName)) {
                String localIPString = "";
                try {
                    List<String> localIPList = IPUtils.getLocalHostLANAddress();
                    System.out.println("获取服务器IP操作成功.IP=" + localIPList);
                    localIPString = localIPList.get(0);
                } catch (Exception e) {
                    System.err.println("获取服务器IP操作失败." + e.getMessage());
                    System.err.println(e);
                    localIPString = "NOIP";
                }
                applicationName =  "PaperlessClient-log-" + localIPString;
            }

            // 加载指定路径logback-cfca.xml
            System.out.println("registe logback-cfca start.");
            try{
                LogbackInitializer.setLogback(externalConfigFilePath, applicationName);
                logger.info("CFCA专用日志组件加载成功[logback-cfca.xml].logbackFilePath=" + externalConfigFilePath + " applicationName=" + applicationName);
            } catch (Exception e) {
                System.err.println("CFCA专用日志组件加载失败[logback-cfca.xml].logbackFilePath=" + externalConfigFilePath + " applicationName=" + applicationName + "." + e.getMessage());
                throw e;
            }
            logger.info("registe logback-cfca end.");
            
       } catch (Exception e) {
           System.err.println(e);
           throw e;
       }
    }
}
